<?php

namespace app\common\cachemodel\chat;

use app\common\model\BaseCache;

class HzQuestionLogic extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.hz_question_logic';

    protected $cacheModel = 'explain.question_logic';

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['question_id' => ['in', $ids]])
                ->order('pid asc,id asc')
                ->column('id,status,question_id,pid,relation_type,logic_type', 'id');

            $resData = [];
            foreach ($data as $item)
            {
                $resData[$item['question_id']][] = [
                    'id' => (int)$item['id'],
                    'status' => (int)$item['status'],
                    'question_id' => (int)$item['question_id'],
                    'pid' => (int)$item['pid'],
                    'relation_type' => (int)$item['relation_type'],
                    'logic_type' => $item['logic_type'],
                ];
            }

            foreach ($resData as $k => $v)
            {
                $resData[$k] = json_encode($v, JSON_UNESCAPED_UNICODE);
            }
        }
        catch (\Exception $e)
        {
            $resData = [];
        }

        return $resData;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = json_decode($itemData, true);
        }

        $dataList = array_merge(...$data);
        // 重新排序    排序规则   pid asc,id asc
        array_multisort(array_column($dataList, 'pid'), SORT_ASC, array_column($dataList, 'id'), SORT_ASC, $dataList);

        return $dataList;
    }
}